﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CFZS.FrameWork.Utility;
using System.Data.SqlClient;
using CFZS.IDAL;
using System.Data;

namespace CFZS.SQLServerDAL
{
    /// <summary>
    /// 数据访问类:CommNews
    /// </summary>
    public partial class CommNews : ICommNews
    {
        public CommNews()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(long ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from CommNews");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.BigInt)
			};
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public long Add(CFZS.Model.CommNews model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into CommNews(");
            strSql.Append("title,img,source,author,summary,content,columntitle,isverified,createdat,createdby,createdbyname,updatedat,updatedby,updatedbyname,ipaddress,site,applyto,browsenum,isdelete,lineno)");
            strSql.Append(" values (");
            strSql.Append("@title,@img,@source,@author,@summary,@content,@columntitle,@isverified,@createdat,@createdby,@createdbyname,@updatedat,@updatedby,@updatedbyname,@ipaddress,@site,@applyto,@browsenum,@isdelete,@lineno)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@title", SqlDbType.VarChar,200),
					new SqlParameter("@img", SqlDbType.VarChar,100),
					new SqlParameter("@source", SqlDbType.VarChar,200),
					new SqlParameter("@author", SqlDbType.VarChar,50),
					new SqlParameter("@summary", SqlDbType.Text),
					new SqlParameter("@content", SqlDbType.Text),
					new SqlParameter("@columntitle", SqlDbType.VarChar,50),
					new SqlParameter("@isverified", SqlDbType.Int,4),
					new SqlParameter("@createdat", SqlDbType.DateTime),
					new SqlParameter("@createdby", SqlDbType.BigInt,8),
					new SqlParameter("@createdbyname", SqlDbType.VarChar,50),
					new SqlParameter("@updatedat", SqlDbType.DateTime),
					new SqlParameter("@updatedby", SqlDbType.BigInt,8),
					new SqlParameter("@updatedbyname", SqlDbType.VarChar,50),
					new SqlParameter("@ipaddress", SqlDbType.VarChar,50),
					new SqlParameter("@site", SqlDbType.VarChar,50),
					new SqlParameter("@applyto", SqlDbType.VarChar,50),
					new SqlParameter("@browsenum", SqlDbType.Int,4),
					new SqlParameter("@isdelete", SqlDbType.Int,4),
					new SqlParameter("@lineno", SqlDbType.BigInt,8)};
            parameters[0].Value = model.title;
            parameters[1].Value = model.img;
            parameters[2].Value = model.source;
            parameters[3].Value = model.author;
            parameters[4].Value = model.summary;
            parameters[5].Value = model.content;
            parameters[6].Value = model.columntitle;
            parameters[7].Value = model.isverified;
            parameters[8].Value = model.createdat;
            parameters[9].Value = model.createdby;
            parameters[10].Value = model.createdbyname;
            parameters[11].Value = model.updatedat;
            parameters[12].Value = model.updatedby;
            parameters[13].Value = model.updatedbyname;
            parameters[14].Value = model.ipaddress;
            parameters[15].Value = model.site;
            parameters[16].Value = model.applyto;
            parameters[17].Value = model.browsenum;
            parameters[18].Value = model.isdelete;
            parameters[19].Value = model.lineno;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt64(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CFZS.Model.CommNews model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update CommNews set ");
            strSql.Append("title=@title,");
            strSql.Append("img=@img,");
            strSql.Append("source=@source,");
            strSql.Append("author=@author,");
            strSql.Append("summary=@summary,");
            strSql.Append("content=@content,");
            strSql.Append("columntitle=@columntitle,");
            strSql.Append("isverified=@isverified,");
            strSql.Append("createdat=@createdat,");
            strSql.Append("createdby=@createdby,");
            strSql.Append("createdbyname=@createdbyname,");
            strSql.Append("updatedat=@updatedat,");
            strSql.Append("updatedby=@updatedby,");
            strSql.Append("updatedbyname=@updatedbyname,");
            strSql.Append("ipaddress=@ipaddress,");
            strSql.Append("site=@site,");
            strSql.Append("applyto=@applyto,");
            strSql.Append("browsenum=@browsenum,");
            strSql.Append("isdelete=@isdelete,");
            strSql.Append("lineno=@lineno");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@title", SqlDbType.VarChar,200),
					new SqlParameter("@img", SqlDbType.VarChar,100),
					new SqlParameter("@source", SqlDbType.VarChar,200),
					new SqlParameter("@author", SqlDbType.VarChar,50),
					new SqlParameter("@summary", SqlDbType.Text),
					new SqlParameter("@content", SqlDbType.Text),
					new SqlParameter("@columntitle", SqlDbType.VarChar,50),
					new SqlParameter("@isverified", SqlDbType.Int,4),
					new SqlParameter("@createdat", SqlDbType.DateTime),
					new SqlParameter("@createdby", SqlDbType.BigInt,8),
					new SqlParameter("@createdbyname", SqlDbType.VarChar,50),
					new SqlParameter("@updatedat", SqlDbType.DateTime),
					new SqlParameter("@updatedby", SqlDbType.BigInt,8),
					new SqlParameter("@updatedbyname", SqlDbType.VarChar,50),
					new SqlParameter("@ipaddress", SqlDbType.VarChar,50),
					new SqlParameter("@site", SqlDbType.VarChar,50),
					new SqlParameter("@applyto", SqlDbType.VarChar,50),
					new SqlParameter("@browsenum", SqlDbType.Int,4),
					new SqlParameter("@isdelete", SqlDbType.Int,4),
					new SqlParameter("@lineno", SqlDbType.BigInt,8),
					new SqlParameter("@ID", SqlDbType.BigInt,8)};
            parameters[0].Value = model.title;
            parameters[1].Value = model.img;
            parameters[2].Value = model.source;
            parameters[3].Value = model.author;
            parameters[4].Value = model.summary;
            parameters[5].Value = model.content;
            parameters[6].Value = model.columntitle;
            parameters[7].Value = model.isverified;
            parameters[8].Value = model.createdat;
            parameters[9].Value = model.createdby;
            parameters[10].Value = model.createdbyname;
            parameters[11].Value = model.updatedat;
            parameters[12].Value = model.updatedby;
            parameters[13].Value = model.updatedbyname;
            parameters[14].Value = model.ipaddress;
            parameters[15].Value = model.site;
            parameters[16].Value = model.applyto;
            parameters[17].Value = model.browsenum;
            parameters[18].Value = model.isdelete;
            parameters[19].Value = model.lineno;
            parameters[20].Value = model.ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(long ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from CommNews ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.BigInt)
			};
            parameters[0].Value = ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from CommNews ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CFZS.Model.CommNews GetModel(long ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,title,img,source,author,summary,content,columntitle,isverified,createdat,createdby,createdbyname,updatedat,updatedby,updatedbyname,ipaddress,site,applyto,browsenum,isdelete,lineno from CommNews ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.BigInt)
			};
            parameters[0].Value = ID;

            CFZS.Model.CommNews model = new CFZS.Model.CommNews();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = long.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["title"] != null && ds.Tables[0].Rows[0]["title"].ToString() != "")
                {
                    model.title = ds.Tables[0].Rows[0]["title"].ToString();
                }
                if (ds.Tables[0].Rows[0]["img"] != null && ds.Tables[0].Rows[0]["img"].ToString() != "")
                {
                    model.img = ds.Tables[0].Rows[0]["img"].ToString();
                }
                if (ds.Tables[0].Rows[0]["source"] != null && ds.Tables[0].Rows[0]["source"].ToString() != "")
                {
                    model.source = ds.Tables[0].Rows[0]["source"].ToString();
                }
                if (ds.Tables[0].Rows[0]["author"] != null && ds.Tables[0].Rows[0]["author"].ToString() != "")
                {
                    model.author = ds.Tables[0].Rows[0]["author"].ToString();
                }
                if (ds.Tables[0].Rows[0]["summary"] != null && ds.Tables[0].Rows[0]["summary"].ToString() != "")
                {
                    model.summary = ds.Tables[0].Rows[0]["summary"].ToString();
                }
                if (ds.Tables[0].Rows[0]["content"] != null && ds.Tables[0].Rows[0]["content"].ToString() != "")
                {
                    model.content = ds.Tables[0].Rows[0]["content"].ToString();
                }
                if (ds.Tables[0].Rows[0]["columntitle"] != null && ds.Tables[0].Rows[0]["columntitle"].ToString() != "")
                {
                    model.columntitle = ds.Tables[0].Rows[0]["columntitle"].ToString();
                }
                if (ds.Tables[0].Rows[0]["isverified"] != null && ds.Tables[0].Rows[0]["isverified"].ToString() != "")
                {
                    model.isverified = int.Parse(ds.Tables[0].Rows[0]["isverified"].ToString());
                }
                if (ds.Tables[0].Rows[0]["createdat"] != null && ds.Tables[0].Rows[0]["createdat"].ToString() != "")
                {
                    model.createdat = DateTime.Parse(ds.Tables[0].Rows[0]["createdat"].ToString());
                }
                if (ds.Tables[0].Rows[0]["createdby"] != null && ds.Tables[0].Rows[0]["createdby"].ToString() != "")
                {
                    model.createdby = long.Parse(ds.Tables[0].Rows[0]["createdby"].ToString());
                }
                if (ds.Tables[0].Rows[0]["createdbyname"] != null && ds.Tables[0].Rows[0]["createdbyname"].ToString() != "")
                {
                    model.createdbyname = ds.Tables[0].Rows[0]["createdbyname"].ToString();
                }
                if (ds.Tables[0].Rows[0]["updatedat"] != null && ds.Tables[0].Rows[0]["updatedat"].ToString() != "")
                {
                    model.updatedat = DateTime.Parse(ds.Tables[0].Rows[0]["updatedat"].ToString());
                }
                if (ds.Tables[0].Rows[0]["updatedby"] != null && ds.Tables[0].Rows[0]["updatedby"].ToString() != "")
                {
                    model.updatedby = long.Parse(ds.Tables[0].Rows[0]["updatedby"].ToString());
                }
                if (ds.Tables[0].Rows[0]["updatedbyname"] != null && ds.Tables[0].Rows[0]["updatedbyname"].ToString() != "")
                {
                    model.updatedbyname = ds.Tables[0].Rows[0]["updatedbyname"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ipaddress"] != null && ds.Tables[0].Rows[0]["ipaddress"].ToString() != "")
                {
                    model.ipaddress = ds.Tables[0].Rows[0]["ipaddress"].ToString();
                }
                if (ds.Tables[0].Rows[0]["site"] != null && ds.Tables[0].Rows[0]["site"].ToString() != "")
                {
                    model.site = ds.Tables[0].Rows[0]["site"].ToString();
                }
                if (ds.Tables[0].Rows[0]["applyto"] != null && ds.Tables[0].Rows[0]["applyto"].ToString() != "")
                {
                    model.applyto = ds.Tables[0].Rows[0]["applyto"].ToString();
                }
                if (ds.Tables[0].Rows[0]["browsenum"] != null && ds.Tables[0].Rows[0]["browsenum"].ToString() != "")
                {
                    model.browsenum = int.Parse(ds.Tables[0].Rows[0]["browsenum"].ToString());
                }
                if (ds.Tables[0].Rows[0]["isdelete"] != null && ds.Tables[0].Rows[0]["isdelete"].ToString() != "")
                {
                    model.isdelete = int.Parse(ds.Tables[0].Rows[0]["isdelete"].ToString());
                }
                if (ds.Tables[0].Rows[0]["lineno"] != null && ds.Tables[0].Rows[0]["lineno"].ToString() != "")
                {
                    model.lineno = long.Parse(ds.Tables[0].Rows[0]["lineno"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,title,img,source,author,summary,content,columntitle,isverified,createdat,createdby,createdbyname,updatedat,updatedby,updatedbyname,ipaddress,site,applyto,browsenum,isdelete,lineno ");
            strSql.Append(" FROM CommNews ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,title,img,source,author,summary,content,columntitle,isverified,createdat,createdby,createdbyname,updatedat,updatedby,updatedbyname,ipaddress,site,applyto,browsenum,isdelete,lineno ");
            strSql.Append(" FROM CommNews ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM CommNews ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.ID desc");
            }
            strSql.Append(")AS Row, T.*  from CommNews T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "CommNews";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}
